package com.haltakov.letterjumble.game.api;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class BoardWord implements Comparable<BoardWord> {
	private List<Letter> letters;
	private int x, y;
	private WordDirection direction;
	private int score;
	private Board board;
	
	public BoardWord(Board board, List<Letter> letters, int x, int y, WordDirection direction) {
		this.board = board;
		
		this.letters = new ArrayList<Letter>(letters.size());
		this.letters.addAll(letters);
		
		this.x = x;
		this.y = y;
		this.direction = direction;
		
		score = this.board.getWordPoints(this);
	}
	
	public BoardWord(Board board, Letter letters[], int x, int y, WordDirection direction) {
		this(board, Arrays.asList(letters), x, y, direction);
	}
	
	public String getText() {
		StringBuilder result = new StringBuilder();
		for (Letter l: letters)
			result.append(l.getCharacter());
		return result.toString();
	}
	
	public List<Letter> getLetters() {
		return Collections.unmodifiableList(letters);
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}
	
	public WordDirection getDirection() {
		return direction;
	}
	
	public void setDirection(WordDirection direction) {
		this.direction = direction;
	}
	
	public int getSize() {
		return letters.size();
	}
	
	@Override
	public String toString() {
		return getText() + "(" + x + "," + y + ")" + " - " + score;
	}
	
	public int getScore() {
		return score;
	}

	@Override
	public int compareTo(BoardWord o) {
		return score-o.score;
	}
}
